<?php

namespace App\Providers;

use Dingo\Api\Facade\API;
use App\Traits\ApiResponse;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\ServiceProvider;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;

class AppServiceProvider extends ServiceProvider
{
    use ApiResponse;

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        if ($this->app->isLocal()) {
            $this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
        }

        //频率错误
        API::error(function (ThrottleRequestsException $exception) {
            $headers = $exception->getHeaders();
            return $this->failed('请求太过频繁，请' . custom_second_trans($headers['Retry-After']) . '后再试！');
        });

        //请求格式错误
        API::error(function (MethodNotAllowedHttpException $exception) {
            return $this->failed('请求方式错误', 400);
        });
    }

    public function registerValidator()
    {
        $validator = $this->app['validator'];

        $validator->extend('phone', function ($attribute, $value, $parameters) {
            return preg_match("/^[1]([3-9])[0-9]{9}$/", $value) ? true : false;
        });

        $validator->extend('phone_code', function ($attribute, $value, $parameters) {

        });

        $validator->extend('code', function ($attribute, $value, $parameters) {
            dd(1111);
        });
    }

    public function registerCustomContainer()
    {
        $this->app->bind(
            \App\Contracts\FileInterface::class,
            \App\Services\FileService::class
        );
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerValidator();

        $this->registerCustomContainer();
    }
}